home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / faultw / handler.asm < prev   
Assembly Source File  |  1992-03-30  |  5KB  |  165 lines

  1. ;
  2. ; HANDLER.ASM
  3. ;
  4. ; ExceptionHandler function used as the callback for the GP Fault Handler.
  5. ;
  6. ; Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  7. ;
  8.  
  9.     .xlist
  10.     ?PLM=1
  11.     ?WIN=1
  12.     include cmacros.inc
  13.     include toolhelp.inc
  14.     include fault.inc
  15.     .list
  16.  
  17.  
  18. createSeg HANDLER_TEXT, HANDLER_TEXT, BYTE, PUBLIC, CODE
  19. sBegin    HANDLER_TEXT
  20. assumes   CS,HANDLER_TEXT
  21. assumes   DS,_DATA
  22.  
  23.  
  24. ;
  25. ; ExceptionHandler
  26. ;
  27. ; Purpose:
  28. ;  Exception handling function called from ToolHelp when it detects
  29. ;  an exception.  If the error was caused by our application AND we
  30. ;  want to trap it, then we use Throw to return control to the function
  31. ;  that faulted at a point BEFORE the erroneous code.  If we do not
  32. ;  want the fault, then we just pass it to the next interrupt handler.
  33. ;
  34. ;  If the fault was caused by our application, we use Throw to return to
  35. ;  whatever function caused it.  Otherwise we just let the fault pass.
  36. ;
  37. ; Parameters:
  38. ;  None; however, the stack contains values of interest.  We use AX to
  39. ;  simply set the DS register properly.
  40. ;
  41. ;     |           .           |
  42. ;     |           .           |
  43. ;     |           .           |
  44. ;     |-----------------------|
  45. ;     |   SS (fault)          |  SP + 12h
  46. ;     |-----------------------|
  47. ;     |   SP (fault)          |  SP + 10h
  48. ;     |-----------------------|
  49. ;     |   Flags (fault)       |  SP + 0Eh
  50. ;     |-----------------------|
  51. ;     |   CS (fault)          |  SP + 0Ch
  52. ;     |-----------------------|
  53. ;     |   IP (fault)          |  SP + 0Ah
  54. ;     |-----------------------|
  55. ;     |   handle (internal)   |  SP + 08h
  56. ;     |-----------------------|
  57. ;     |   interrupt number    |  SP + 06h
  58. ;     |-----------------------|
  59. ;     |   AX (NOT the DS)     |  SP + 04h
  60. ;     |-----------------------|
  61. ;     |   CS (toolhelp.dll)   |  SP + 02h
  62. ;     |-----------------------|
  63. ;     |   IP (toolhelp.dll)   |  SP + 00h
  64. ;     +-----------------------+
  65. ;
  66. ;  Note that the interrupt number may have the high bit set to
  67. ;  indicate a low-stack fault (above and beyond a normal Int 12 stack)
  68. ;  fault.  We trap this condition if we're trapping a stack fault or a
  69. ;  GP fault.
  70. ;
  71.  
  72.  
  73. cProc   ExceptionHandler, <PUBLIC,FAR>
  74.  
  75. cBegin  nogen
  76.  
  77.         mov     ds,ax               ;Make sure we can reference our data.
  78.  
  79.         mov     ax,bp
  80.         mov     bp,sp
  81.         mov     bx,[bp+06]
  82.         mov     bp,ax
  83.  
  84.         ;
  85.         ; Cycle through the possible faults we are looking for.
  86.         ; If BX contains that fault, then we use Throw to return
  87.         ; the error to the application.  Otherwise we pass the
  88.         ; fault down the chain.
  89.         ;
  90.         ; First, the high bit might be set in the interrupt meaning
  91.         ; that we have a low-stack fault.  Since we do not handle
  92.         ; these, we exit this handler.
  93.         ;
  94.  
  95.         test    bx,08000h           ;Check high bit
  96.         jnz     EHExit              ;Leave it it's set--we can't handle it.
  97.  
  98.         mov     ax,_wException      ;Get the exceptions we want to trap.
  99.         or      ax,ax               ;Do we want any?
  100.         jz      EHExit
  101.  
  102.  
  103. EHDivideByZero:
  104.         ;
  105.         ; Check for divide by zero.
  106.         ;
  107.  
  108.         test    ax,EXCEPTION_DIVIDEBYZERO
  109.         jz      EHGPFault
  110.         or      bx,bx
  111.         jz      EHThrow
  112.  
  113.  
  114. EHGPFault:
  115.         ;
  116.         ; Check for a GP fault.
  117.         ;
  118.  
  119.         test    ax,EXCEPTION_GPFAULT
  120.         jz      EHExit
  121.         cmp     bx,13
  122.         je      EHThrow
  123.  
  124.  
  125. EHThrow:
  126.         ccall   Throw,<_pcbEx, 1>  ;Return the exception to the faulting
  127.                                     ;function.
  128.  
  129. EHExit:
  130.         retf
  131.  
  132.  
  133. EHRestart:
  134.         ;
  135.         ; This code illustrates what we need to do if we wanted to
  136.         ; clean up the problem and restart the instruction that faulted.
  137.         ; This exception handler does not use this code.
  138.         ;
  139.  
  140.         add     sp,10                   ;Clear the return data on the stack.
  141.         iret                            ;Return to the faulting instruction.
  142.  
  143.  
  144. EHTerminate:
  145.         ;
  146.         ; This code illustrates what we need to do if we wanted to
  147.         ; terminate the applciation on this exception.  This exception
  148.         ; handler does not use this code.
  149.         ;
  150.  
  151.         add     sp,10                   ;Clear the return data on the stack.
  152.  
  153.         ;
  154.         ; With TerminateApp you have the choice to display the standard
  155.         ; UAE box or not.  This sample does since it otherwise would give
  156.         ; no indication of the problem.
  157.         ;
  158.         cCall   TerminateApp, <0, UAE_BOX>
  159.  
  160. cEnd    nogen
  161.  
  162. sEnd    HANDLER_TEXT
  163.  
  164.         END
  165.